package youbff.cloud.test.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import redis.clients.jedis.JedisPoolConfig;
import youbff.cloud.core.utils.RedisUtil;
import youbff.cloud.core.utils.SessionUtils;

/**
 * redis 配置
 * @author lixuefeng
 * @email youbff@163.com
 * @time 2017年11月12日 下午10:42:23
 */
@Configuration
@Order(1)
public class RedisConfig extends CachingConfigurerSupport {
    
    @Value("${redis.maxIdle}")
    private Integer maxIdle;
    @Value("${redis.maxWaitMillis}")
    private Integer maxWaitMillis;
    @Value("${redis.hostName}")
    private String hostName;
    @Value("${redis.port}")
    private Integer port;
    @Value("${redis.password}")
    private String password;

    @Bean
    public JedisPoolConfig jedisPoolConfig(){
        JedisPoolConfig jedisPool = new JedisPoolConfig();
        jedisPool.setMaxIdle(maxIdle);
        jedisPool.setMaxWaitMillis(maxWaitMillis);
        jedisPool.setTestOnBorrow(true);
        return jedisPool;
    }
    
    @Bean
    public JedisConnectionFactory jedisConnectionFactory(){
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
        config.setHostName(hostName);
        config.setPassword(RedisPassword.of(password));
        config.setPort(port);
        JedisConnectionFactory factory = new JedisConnectionFactory(config);
        return factory;
    }
    
    @Bean(name="redisTemplate")
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public RedisTemplate redisTemplate(){
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(jedisConnectionFactory());
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        return redisTemplate;
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Bean(name="redisUtil")
    @DependsOn(value={"redisTemplate"})  
    public RedisUtil redisUtil(@Qualifier("redisTemplate") RedisTemplate redisTemplate){
        RedisUtil redisUtil = new RedisUtil();
        redisUtil.setRedisTemplate(redisTemplate);
        SessionUtils.setRedisUtil(redisUtil);
        return redisUtil;
    }
    
}
